import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";
import viteCompression from "vite-plugin-compression";

// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		Components({
			resolvers: [VantResolver()]
		}),
		viteCompression({
			threshold: 1024, // 对超过改大小B的才进行压缩
			algorithm: "gzip", // 压缩算法
			verbose: true, // 是否在控制台中输出压缩结果
			ext: ".gz", // 生成的压缩包的后缀
			deleteOriginFile: true, // 源文件压缩后是否删除
			filter: /\.(js|mjs|json|css)$/i
		})
	],
	resolve: {
		alias: {
			// @ts-ignore
			"@": fileURLToPath(new URL("./src", import.meta.url))
		}
	},
	build: {
		rollupOptions: {
			output: {
				manualChunks: (id) => {
					// 将 node_modules 中的代码单独打包成一个 JS 文件
					if (id.includes("node_modules")) {
						return "vendor";
					}
				}
			}
		}
	},
	base: "/"
});
